﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSConsoleTest
{
    class TestFile : BaseConsoleTest
    {
        public void printDescription()
        {
            Console.WriteLine("--- File ---");
        }

        public void doTest()
        {
            printDirectory("D:\\");
            writeAndReadLongValue("D:\\temp\\long_value_test1.dat", 0x123456789ABCDEF0);
            writeAndReadLongValueWithBinaryHelper("D:\\temp\\long_value_test2.dat", 0x123456789ABCDEF0);
            writeAndReadLongValueWithStreamHelper("D:\\temp\\long_value_test3.dat", 0x123456789ABCDEF0);
        }

        private void printDirectory(string aDirPath)
        {
            Console.WriteLine("- Print directory : {0}", aDirPath);

            string[] dirs = Directory.GetDirectories(aDirPath);
            foreach (string dir in dirs)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                Console.WriteLine("{0} : {1}", dirInfo.Name, dirInfo.Attributes);
            }
        }

        private void writeAndReadLongValue(string filePath, long value)
        {
            Console.WriteLine("- Write and read long value : {0} :: value : {1}", filePath, value);
            FileInfo targetFile = new FileInfo(filePath);
            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            byte[] bytesOfLong = BitConverter.GetBytes(value);
            FileStream targetStream = targetFile.Create();
            targetStream.Write(bytesOfLong, 0, bytesOfLong.Length);
            targetStream.Close();

            Console.WriteLine("Written value : {0}", value);

            byte[] readBytes = new byte[30];
            FileStream targetReadStream = targetFile.OpenRead();
            targetReadStream.Read(readBytes, 0, readBytes.Length);
            targetReadStream.Close();
            long readValue = BitConverter.ToInt64(readBytes, 0);

            Console.WriteLine("Read value : {0}", readValue);
        }

        private void writeAndReadLongValueWithBinaryHelper(string filePath, long value)
        {
            Console.WriteLine("- Write and read long value (BinaryHelper) : {0} :: value : {1}", filePath, value);
            FileInfo targetFile = new FileInfo(filePath);
            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            FileStream targetStream = targetFile.Create();
            BinaryWriter writer = new BinaryWriter(targetStream);

            writer.Write(value);
            writer.Close();

            Console.WriteLine("Written value : {0}", value);

            byte[] readBytes = new byte[30];
            FileStream targetReadStream = targetFile.OpenRead();
            BinaryReader reader = new BinaryReader(targetReadStream);
            long readValue = reader.ReadInt64();
            reader.Close();

            Console.WriteLine("Read value : {0}", readValue);
        }

        private void writeAndReadLongValueWithStreamHelper(string filePath, long value)
        {
            Console.WriteLine("- Write and read long value (StreamHelper) : {0} :: value : {1}", filePath, value);
            FileInfo targetFile = new FileInfo(filePath);
            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            FileStream targetStream = targetFile.Create();
            StreamWriter writer = new StreamWriter(targetStream);

            writer.Write(value);
            writer.Close();

            Console.WriteLine("Written value : {0}", value);

            byte[] readBytes = new byte[30];
            FileStream targetReadStream = targetFile.OpenRead();
            StreamReader reader = new StreamReader(targetReadStream);
            string strReadValue = reader.ReadLine();
            long readValue = long.Parse(strReadValue);
            reader.Close();

            Console.WriteLine("Read value : {0}", readValue);
        }
    }
}
